package cn.sxt.yxl.controller.head;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sxt.yxl.common.util.ConstatFinalUtil;
import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.pojo.AColumns;
import cn.sxt.yxl.question.pojo.AItems;
import cn.sxt.yxl.question.pojo.AQuestion;
import cn.sxt.yxl.question.service.IQuestionService;
import cn.sxt.yxl.question.service.impl.QuestionServiceImpl;

/**
 * 首页模块的Contrller
 * 不需要登陆
 * @author wangshSxt
 *
 */
@Controller
public class IndexHeadController{
	
	@Autowired
	private IQuestionService questionService;
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("---- main ----");
		return "/head/main" ; 
	}
	/**
	 * 读取columns列表
	 */
	@RequestMapping("/readColumns")
	public String readColumns(HttpServletRequest request){
		/*查询条件*/
		Map<String, Object> condMap=new HashMap<String ,Object>();
		condMap.put("stutas", "1");
		List<AColumns> columnsList=questionService.findCondListColumnsService(null, condMap);
		
		request.setAttribute("columnsList", columnsList);
		
		return "/head/header";
	}
	/**
	 * 展示columns下面的items
	 * @return
	 */
	@RequestMapping("/{url}")
	public String itemList(@PathVariable String url,HttpServletRequest request){
		ConstatFinalUtil.LOGGER.info("----itemList start----url:{}",url);
		/*获取参数*/
		String page=request.getParameter("page");
		ConstatFinalUtil.LOGGER.info("----itemList start----page:{}",page);
		/*根据url查询columns*/
		Map<String, Object> condMap=new HashMap<String,Object>();
		condMap.put("url", url);
		AColumns columns=questionService.findOneColumnsService(condMap);
		int columnsId=columns.getId();
		
		/*根据columns的id查询其下的items*/
		condMap.clear();
		condMap.put("columnsId", columnsId);
		/*设置分页条件*/
		PageInfoUtil pageInfoUtil=new PageInfoUtil();
		try {
			pageInfoUtil.setCurrentPage(Integer.valueOf(page));
		} catch (NumberFormatException e) {
		}
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		List<AItems> itemsList=questionService.findCondListItemsService(pageInfoUtil, condMap);
		request.setAttribute("url", url);
		request.setAttribute("columns", columns);
		request.setAttribute("itemsList", itemsList);
		return "head/itemList";
	}
	/**
	 * 打开item展示question
	 * @return
	 */
	@RequestMapping("/question/{id}")
	public String question(@PathVariable int id,HttpServletRequest request){
		ConstatFinalUtil.LOGGER.info("----question start----id:{}",id);
		
		Map<String, Object> condMap=new HashMap<String,Object>();
		condMap.put("id", id);
		/*查询一条栏目*/
		AItems items =questionService.findOneItemsService(condMap);
		request.setAttribute("items", items);
		
		return "head/question";
	}
	/**
	 * 答题页面
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/questionList/{id}")
	public String listAnswer(@PathVariable int id,HttpServletRequest request){
		ConstatFinalUtil.LOGGER.info("----listAnswer start----id:{}",id);
		Map<String, Object> condMap=new HashMap<String,Object>();
		condMap.put("id", id);
		AItems items=questionService.findOneItemsService(condMap);
		request.setAttribute("items", items);
		
		return "head/questionList";
	}
	
}
